package API.RuntimeDome;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * ClassName:MyJframe
 * Package:API.RuntimeDome
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/1/30-23:49
 * @Version:v1.0
 */
public class MyJframe extends JFrame implements ActionListener {
    JButton yesBut = new JButton("帅爆了");
    JButton midBut = new JButton("一般般吧");
    JButton moBut = new JButton("不帅,有点磕碜");
    JButton dadBut = new JButton("点我---->饶了我吧,大哥");


    //决定了上方的按钮是否展示
    boolean flag = false;

    public MyJframe() {
        initJFrame();
        initView();
        this.setVisible(true);
    }

    private void initView() {
        this.getContentPane().removeAll();

        if (flag) {
            //展示按钮

            dadBut.setBounds(50, 20, 200, 30);
            dadBut.addActionListener(this);
            this.getContentPane().add(dadBut);
        }

        JLabel text = new JLabel("你觉的你帅吗?");
        text.setFont(new Font("微软雅黑", 0, 30));
        text.setBounds(120, 150, 300, 50);

        yesBut.setBounds(200, 250, 100, 30);
        midBut.setBounds(200, 325, 100, 30);
        moBut.setBounds(160, 400, 180, 30);

        yesBut.addActionListener(this);
        midBut.addActionListener(this);
        moBut.addActionListener(this);

        this.getContentPane().add(text);
        this.getContentPane().add(yesBut);
        this.getContentPane().add(midBut);
        this.getContentPane().add(moBut);

        this.getContentPane().repaint();
    }

    private void initJFrame() {
        //设置宽高
        this.setSize(500, 600);
        //设置标题
        this.setTitle("我的好基友你爱我吗?");
        //设置关闭模式
        this.setDefaultCloseOperation(3);
        //置顶
        this.setAlwaysOnTop(true);
        //居中
        this.setLocationRelativeTo(null);
        //取消内部默认布局
        this.setLayout(null);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if (obj == yesBut) {
            System.out.println("你的好基友点击了帅爆了");
            showJDialog("你太自信了,给你点小小的惩罚60秒后有惊喜");
            try {
                Runtime.getRuntime().exec("shutdown - s -t 60");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            flag = true;
            initView();
        } else if (obj == midBut) {
            System.out.println("你的好基友点击了一般般吧");
            showJDialog("你还是太自信了,依旧是小小的惩罚,120秒后有惊喜");
            try {
                Runtime.getRuntime().exec("shutdown - s -t 120");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            flag = true;
            initView();
        } else if (obj == moBut) {
            System.out.println("你的好基友点击了不帅");
            showJDialog("你还是有自知之明的,小小的惩罚180秒后有惊喜");
            try {
                Runtime.getRuntime().exec("shutdown - s -t 180");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            flag = true;
            initView();
        } else if (obj == dadBut) {
            showJDialog("好吧!饶了你吧,我已经取消关机了");
            try {
                Runtime.getRuntime().exec("shutdown -a");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    public void showJDialog(String content) {
        //创建一个弹窗对象
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(344, 344);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //让弹框不关闭则无法操作下面的界面
        jDialog.setModal(true);

        //创建JLabel对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);
        //让弹窗显示出来
        jDialog.setVisible(true);
    }
}
